//
//  FR_ClassesPicker.m
//  FaceRecognition
//
//  Created by zz on 17/7/17.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_ClassesPicker.h"
#import "ManagerInfoModel.h"
@interface FR_ClassesPicker() <UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *picker;

@property (nonatomic, strong) UIToolbar *toolBar;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation FR_ClassesPicker

#pragma mark -Setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    
    _dataArr = dataArr;
    [self.picker reloadAllComponents];
}

#pragma mark -LifeCycle
+ (instancetype)showWithSuperView:(UIView *)superView {
    
    FR_ClassesPicker *pickView = [[self alloc] initWithFrame:superView.bounds];
    
    return pickView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _selectIndex = 0;
        
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, frame.size.height - 170, frame.size.width, 170)];
        _picker.delegate = self;
        _picker.dataSource = self;
        _picker.backgroundColor = [UIColor whiteColor];
        [self addSubview:_picker];
        
        _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(_picker.frame) - 35, SCREENWIDTH, 35)];
        _toolBar.backgroundColor = [UIColor whiteColor];
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStyleDone target:self action:@selector(tapAction)];
        UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
        confirmBtn.frame = CGRectMake(SCREENWIDTH - 12 - 50, 12, 50, 15);
        [confirmBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        
        
//        UIBarButtonItem *sureBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成 " style:UIBarButtonItemStyleDone target:self action:@selector(sureAction)];
//        [cancelBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#3b3b3b"]} forState:UIControlStateNormal];
//        [sureBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBACOLOR(57, 173, 255, 1)} forState:UIControlStateNormal];
        UIBarButtonItem *sureBtn = [[UIBarButtonItem alloc] initWithCustomView:confirmBtn];
        
        
        NSArray * buttonsArray = [NSArray arrayWithObjects:cancelBtn,btnSpace,sureBtn,nil];
        [_toolBar setItems:buttonsArray];
        [self addSubview:_toolBar];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
    }
    
    return self;
}

#pragma mark -UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.dataArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    ManagerInfoModel *model = [self.dataArr objectAtIndex:row];
    
    return model.classname;
}

#pragma mark -UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return 200;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 30;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *label = (UILabel *)view;
    
    if (!label) {
        label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = YES;
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
    }
    
    label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    _selectIndex = row;
}

- (void)tapAction {
    
//    [self removeFromSuperview];
    
    if (_confirmBlock) {
        
        _confirmBlock(nil,nil);
        
        [self removeFromSuperview];
    }
}

- (void)sureAction {
    
    if (_confirmBlock) {
        
        ManagerInfoModel *model = [self.dataArr objectAtIndex:_selectIndex];
        _confirmBlock(model.classname,model.classid);
        
        [self removeFromSuperview];
    }
}

@end
